using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums;
using AspNetForums.Components;
using AspNetForums.Controls;

namespace AspNetForums.Admin
{
	/// <summary>
	/// Summary description for ManageForumPermissions.
	/// </summary>
	public class ManageForumPermissions : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Repeater forumGroupRepeater;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Bind the data
			//
			DataBindForumGroups();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/*******************************************************************
		// DataBindForumGroups
		//
		/// <summary>
		/// Databinds the forum group repeater
		/// </summary>
		//
		********************************************************************/
		public void DataBindForumGroups() {
	
			forumGroupRepeater.DataSource = ForumGroups.GetForumGroups(false, true);
			forumGroupRepeater.DataBind();

		}    
         
    
		/*******************************************************************
		// AddRoleToForum_Click
		//
		/// <summary>
		/// Add a new role to a forum
		/// </summary>
		//
		********************************************************************/
		public void AddRoleToForum_Click(Object sender, EventArgs e) {

			Button b = (Button) sender;
        
			int forumID = 0;
			int roleID = 0; 
        
			ForumPermissions.AddForumPermission(forumID,roleID);

			DataBindForumGroups();
		}
    
		/*******************************************************************
		// RemoveRole_Click
		//
		/// <summary>
		/// Remove a new role from a forum
		/// </summary>
		//
		********************************************************************/
		public void RemoveRole_Click(Object sender, EventArgs e) {

			Button b = (Button) sender;
			DropDownList d = (DropDownList) b.Parent.FindControl("RoleIDToAdd");
        
			int forumID = Convert.ToInt32(b.CommandArgument);
			int roleID = Convert.ToInt32(b.CommandName);
        
			ForumPermission permission = new ForumPermission();
			permission.ForumID = forumID;
			permission.RoleID = roleID;
        
			ForumPermissions.RemoveForumPermission(permission);

			DataBindForumGroups();
		}
 
		/*******************************************************************
		// AddRole_Click
		//
		/// <summary>
		/// Add a new role to a Forum
		/// </summary>
		//
		********************************************************************/
		public void AddRole_Click( Object sender, EventArgs e ) {
	
			Button b = (Button)sender;
			DropDownList d = (DropDownList) b.Parent.FindControl("ddlRoles");
		
			int forumID = Convert.ToInt32(b.CommandArgument);
			int roleID = Convert.ToInt32(d.SelectedItem.Value);

			ForumPermission permission = ForumPermissions.GetForumPermission( forumID, roleID );
			if( permission == null ) {
				ForumPermissions.AddForumPermission( forumID, roleID );
			}
		
			DataBindForumGroups();
		
		}
    
	}
}
